package com.example.demo0630.repo;

import com.example.demo0630.po.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 使用@Query注解可以自定义JPQL语句实现更灵活的查询。
 *
 * @author lzh
 * @date 2020/6/30 - 14:38
 */
@Repository
public interface UserRepository extends JpaRepository<User, Integer> {


    /**
     * select user0_.id as id1_0_, user0_.email as email2_0_, user0_.name as name3_0_, user0_.nick_name as nick_nam4_0_
     * from sys_user user0_
     * where user0_.name=? and user0_.email=?
     *
     * @param name
     * @param email
     * @return
     */
    List<User> findByNameAndEmail(String name, String email);

    /**
     * nativeQuery用法 value=”mysql语句 ,参数用占位符 ?1 ?2 ....“
     * @param id
     * @param name
     * @param email
     * @return
     */
    @Query(nativeQuery = true, value = "select * from sys_user where id = ?1 and name = ?2 and email = ?3 ")
    List<User> findCondition(Integer id, String name, String email);


    /**
     * jpql用法 这里的 User 代指 sys_user 表
     * 参数用占位符 可用  :paramName  ,也可用  ?1 ?2 ...
     *
     * @param name
     * @return
     */
    @Query("select  u from  User u where u.name like CONCAT('%',:msg,'%') ")
    List<User> findByNameLike(@Param("msg") String name);



}
